//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: baseJs.imgUrl,
    pageIndex: 0,
    state: "",
    discountArr: [],
    imgArr: ['../../images/discount/eat.jpg', '../../images/discount/half.jpg', '../../images/discount/car.jpg', '../../images/discount/hotel.jpg',],
    // 
    uid:"",
    userType:"",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      userType: wx.getStorageSync('userType')
    })
    this.GetBuluoArea();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.data.pageIndex++;
    this.GetBuluoArea();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //获取折扣专区
  GetBuluoArea: function() {
    var that = this;
    var uid = this.data.uid;
    var pageIndex = this.data.pageIndex;
    var discountArr = this.data.discountArr;
    var imgArr = this.data.imgArr;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetBuluoArea&PageSize=10&PageIndex=" + pageIndex + "&UID=" + uid),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        for (var i = 0; i < json.length; i++) {
          discountArr.push(json[i])
        }
        for (var i = 0; i < discountArr.length; i++) {
          discountArr[i].img = imgArr[i];
        }
        that.setData({
          discountArr: discountArr,
        })
        console.log(discountArr);
        if (json.length == 10) {
          that.setData({
            state: 'loadMore',
          })
        } else if (json.length == 0 && pageIndex == 1) {
          that.setData({
            state: 'noData',
          })
        } else if (json.length < 10) {
          that.setData({
            state: 'noMore',
          })
        } else if (json.length == 0 && pageIndex > 1) {
          that.setData({
            state: 'noMore',
          })
        }

      }
    })
  },
  // 进入专区详情
  leftTap: function(e) {
    console.log(e)
    wx.navigateTo({
      url: '/packageA/discountDetail/discountDetail?discountId=' + e.currentTarget.dataset.discountid + "&discountName=" + e.currentTarget.dataset.discountname,
    })
  },
  // 进入专区会员
  rightTap: function(e) {
    wx.navigateTo({
      url: '/packageA/discountVip/discountVip?discountId=' + e.currentTarget.dataset.discountid,
    })
  },
 
  // 
})